package com.kai.base.fragment

import android.content.Context
import android.graphics.Bitmap
import android.graphics.drawable.Drawable
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.fragment.app.Fragment
import androidx.viewbinding.ViewBinding
import com.hwangjr.rxbus.RxBus
import com.kai.base.base.basic.IView
import com.kai.base.base.basic.ViewDelegate
import com.kai.base.widget.PlaceholderView


/**
 * @Author: 小强同学
 * @Date:  2021/1/19 9:26
 * @Description:  java类作用描述
 */
abstract class BaseFragment<VB : ViewBinding>: Fragment(), IView {
    private lateinit var mDelegate: ViewDelegate
    protected var mRootView: VB? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        if (isRegisterRxBus()) RxBus.get().register(this)
    }


    override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        mRootView =getBinding(inflater,container)
       return if (mRootView!=null){
           mDelegate = ViewDelegate(mRootView!!.root)
           mRootView!!.root
        }else{
            null
        }
    }

      override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initViews()
        initEvents()
    }

    override fun onDestroyView() {
        super.onDestroyView()
        mRootView=null
        if (isRegisterRxBus()) {
            RxBus.get().unregister(this)
        }
    }

    protected abstract fun getBinding(inflater: LayoutInflater, viewGroup: ViewGroup?): VB

    open fun initViews() {}

    open fun initEvents() {}

    /**
     * 是否注册RxBus
     */
    protected open fun isRegisterRxBus(): Boolean = false

    /**
     * 占位控件
     */
    open fun getPlaceholderView(): PlaceholderView? = null


    override fun getContext(): Context? = mDelegate.getContext()

    override fun setVisibility(view: View?, visibility: Int) {
        mDelegate?.setVisibility(view, visibility)
    }

    override fun setVisibility(view: View?, isVisible: Boolean) {
        mDelegate.setVisibility(view, isVisible)
    }

    override fun setVisible(view: View?) {
        mDelegate.setVisible(view)
    }

    override fun setGone(view: View?) {
        mDelegate.setGone(view)
    }

    override fun setText(textView: TextView?, text: CharSequence?) {
        mDelegate.setText(textView, text)
    }

    override fun setText(textView: TextView?, resId: Int) {
        mDelegate.setText(textView, resId)
    }

    override fun setTextColor(textView: TextView?, color: Int) {
        mDelegate.setTextColor(textView, color)
    }

    override fun setTextSize(textView: TextView?, size: Float) {
        mDelegate.setTextSize(textView, size)
    }

    override fun setImageResource(imageView: ImageView?, resId: Int) {
        mDelegate.setImageResource(imageView, resId)
    }

    override fun setImageBitmap(imageView: ImageView?, bitmap: Bitmap?) {
        mDelegate.setImageBitmap(imageView, bitmap)
    }

    override fun setImageDrawable(imageView: ImageView?, drawable: Drawable?) {
        mDelegate.setImageDrawable(imageView, drawable)
    }

    override fun showToast(content: String?) {
        mDelegate.showToast(content)
    }

    override fun showToast(resId: Int) {
        mDelegate.showToast(resId)
    }

    override fun showCustomToast(content: String?) {
        mDelegate.showCustomToast(content)
    }

    override fun showCustomToast(resId: Int) {
        mDelegate.showCustomToast(resId)
    }

    override fun showLongToast(content: String?) {
        mDelegate.showLongToast(content)
    }

    override fun showLongToast(resId: Int) {
        mDelegate.showLongToast(resId)
    }

    override fun showDialogToast(resId: Int) {
        mDelegate.showDialogToast(resId)
    }

    override fun showDialogToast(content: String?) {
        mDelegate.showDialogToast(content)
    }

    override fun showDialogToast(content: String?, duration: Long) {
        mDelegate.showDialogToast(content, duration)
    }

    protected open fun showLoading() {
        mDelegate.showPageLoading(getPlaceholderView())
    }

    protected open fun showEmpty(msg: String? = null) {
        mDelegate.showPageEmpty(getPlaceholderView(), msg)
    }

    protected open fun showError(msg: String? = null) {
        mDelegate.showPageError(getPlaceholderView(), msg)
    }

    protected open fun showContent() {
        mDelegate.showPageContent(getPlaceholderView())
    }

    protected fun setErrorRetryListener(listener: () -> Unit) {
        mDelegate.setPageErrorRetryListener(getPlaceholderView(), listener)
    }

}